﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using System.Web.UI;
using shfa.snomtastic.data.security;
using System.Net;
using shfa.snomtastic.data;
using System.Threading;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class ePhones : EditBase
    {
        #region Public Members

        public AppConfig ac = new AppConfig();
      
        #endregion

        #region Page

       

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridItems();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "FilterEvents", GetQuickSearchScript(), true);
        }


        private string GetQuickSearchScript()
        {
             

            string script = "";
            script += "function onQuickSearch(event, column, text)";
            script += "{";
            script += "var key = (window.event) ? window.event.keyCode : e.which;";
            script += "if (key == 13)";
            script += "  {";
            script += "    var masterTable = $find('" + rgListItems.ClientID + "').get_masterTableView();";
            script += "    masterTable.filter(column, text, 1, true);";
            script += "  }";
            script += "}     ";

            return script;
        }

        #endregion

        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = ((PhoneInfo)e.Item.DataItem).PhoneId;
                RaiseNavEvent(this, new NavEventArgs("ePhone", id));

            }
        }

        #endregion


        public void BindGridItems()
        {
            PhoneController pc = new PhoneController();
            List<PhoneInfo> piList = pc.SelectPhones("MacAddress Asc");

            UserController uc = new UserController();

            foreach (PhoneInfo pi in piList)
            {
                pi.CreatedDate = pi.CreatedDate.ToLocalTime();
                if (pi.UserId != Guid.Empty)
                {
                    UserInfo ui = uc.SelectUser(pi.UserId);
                    if (ui != null)
                    {
                        pi.UserName = ui.UserName;
                    }
                }

            }

            rgListItems.DataSource = piList;
            rgListItems.DataBind();


        }



        #region Helpers

        private void SendPhoneCommand(PhoneCommand.CommandType cmd)
        {
            RijndaelEnhanced re = new RijndaelEnhanced();
            foreach (GridItem item in rgListItems.SelectedItems)
            {
                PhoneCommand phCmd = new PhoneCommand((PhoneInfo)item.DataItem, cmd);
                Thread oThread = new Thread(new ThreadStart(phCmd.SendCommand));
                oThread.Start();
            }
        }

        


        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Refresh":
                    RaiseNavEvent(this, new NavEventArgs("ePhones", new Guid()));
                    break;
                case "Reboot":
                    SendPhoneCommand(PhoneCommand.CommandType.Reboot);
                    RaiseNavEvent(this, new NavEventArgs("ePhones", new Guid()));
                    break;
                case "Register":
                    SendPhoneCommand(PhoneCommand.CommandType.Reregister);
                    RaiseNavEvent(this, new NavEventArgs("ePhones", new Guid()));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get 
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Refresh", "Refresh Phone List", "~/images/icons/refresh.png"));
                al.Add(new ActionInfo("Register", "Re-Register Selected", "~/images/icons/user.png"));
                al.Add(new ActionInfo("Reboot", "Reboot Selected", "~/images/icons/reboot.png", "Are you sure you wish to reboot the selected phones?", true));

                return al;
            }
        }

        #endregion

    }
}